﻿Imports DTO
Imports BUS
Public Class frmPhieuNhapSach
    Private ListDanhSach As List(Of PHieuNhapSach) = New List(Of PHieuNhapSach)
    Private Sub frmPhieuNhapSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '' Load noi dung cua comboBox_ The loai tu CSDL len
        Dim TheLoaiDTO As List(Of BangTheLoaiSachDTO) = New List(Of BangTheLoaiSachDTO)

        TheLoaiDTO = TheLoaiSachBUS.LayTheLoai()
        ComboBox_TheLoai.DataSource = TheLoaiDTO
        ComboBox_TheLoai.DisplayMember = "TenTheLoai"
        ComboBox_TheLoai.ValueMember = "MaTheLoai"
        ComboBox_TheLoai.SelectedIndex = 0
    End Sub
    Public Sub TaoMoi()
        txtSach.Text = ""
        txtSoLuong.Text = ""
        txtTacGia.Text = ""
        ComboBox_TheLoai.Text = ""

    End Sub
    '---------------------------------------Kiem Tra Số Luong Nhập-------------------------------------
    ' So Luong Chi duoc nhap so va so dó phai lon hon >0
    Private Function KiemTraDuong(ByVal n As String) As Boolean
        Try
            Dim soluong As Double = Double.Parse(n)
            If (soluong < 0) Then
                Return True
            End If
        Catch ex As Exception
            Return True

        End Try
        Return False
    End Function
    Private Sub txtSoLuong_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSoLuong.KeyPress, txt_DonGia.KeyPress
        Dim SoLuong As String = txtSoLuong.Text + e.KeyChar.ToString()
        If Char.IsControl(e.KeyChar) Then
            Return
        Else
            e.Handled = KiemTraDuong(SoLuong)
        End If
    End Sub
    '---------------------Kiem tra rong-------------------------------------------------
    Public Function Kiemtrarong(ByVal tenSach As String, ByVal tacGia As String, ByVal TheLoai As String, ByVal SoLuong As String, ByVal DonGia As String) As Integer
        If (tenSach = "") Then
            Return 1
        End If
        If (tacGia = "") Then
            Return 2
        End If
        If (TheLoai = "") Then
            Return 3
        End If
        If (SoLuong = "") Then
            Return 4
        End If
        If DonGia = "" Then
            Return 5
        End If
        Return 0
    End Function

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim kq As Integer = Kiemtrarong(txtSach.Text, txtTacGia.Text, ComboBox_TheLoai.Text, txtSoLuong.Text, txt_DonGia.Text)
        If (kq = 1) Then

            MessageBox.Show("Ten Sach Con trong kia")
            txtSach.Focus()
            Return
        End If
        If kq = 2 Then

            MessageBox.Show("Chua Nhap ten tac gia")
            txtSach.Focus()
            Return
        End If
        If (kq = 3) Then

            MessageBox.Show("Ban chua chon the loai")
            ComboBox_TheLoai.Focus()
            Return
        End If
        If kq = 4 Then

            MessageBox.Show(" Ban Chua chon so luong")
            txtSoLuong.Focus()
            Return
        End If
        If kq = 5 Then
            MessageBox.Show("Ban Chua Nhap Don Gia")
            Return
        End If
        Dim flay As Boolean = True
        '-------------------- Lay Ra so Luong Sach Ton Trong Kho ------------------------------------


        '-------------------Kiem tra so luong nhap phai lon hon so luong NHAP TOI THIEU---------------
        Dim soluong As Double = Double.Parse(txtSoLuong.Text)
        Dim ThamSoBUS = New ThamSoBUS()
        Dim SoLuongNhapToiThieu = ThamSoBUS.LaySoLuongNhapToiThieu()
        If (soluong < SoLuongNhapToiThieu) Then
            MessageBox.Show("So Luong Nhap Thap nhat phai la:" + SoLuongNhapToiThieu.ToString())
            txtSoLuong.Focus()
            Return

        End If


        ' --------- Lay so luong Ton tối đa------------------------------
        Dim soluongtontoida As Double
        soluongtontoida = ThamSoBUS.LaySoLuongTonToiDa()

        ' ---------- Lay so Luong sach da nhap--------------------------------
        Dim PhieuSachDTO As PHieuNhapSach = New PHieuNhapSach()
        PhieuSachDTO.TenSach = txtSach.Text
        PhieuSachDTO.TacGia = txtTacGia.Text
        PhieuSachDTO.TheLoai = ComboBox_TheLoai.Text
        PhieuSachDTO.SoLuong = Double.Parse(txtSoLuong.Text)
        PhieuSachDTO.DonGia = Double.Parse(txt_DonGia.Text)

        Dim soluongsachdanhap As Double = SachBUS.SoLuongTon(PhieuSachDTO)
        ' Kiem tra
        If (soluongsachdanhap > soluongtontoida) Then
            MessageBox.Show(" Trong Tu Sach Da co :       " + soluongsachdanhap.ToString() + " cuon sach " + txtSach.Text + "   so voi so Luong ton toi da la :       " + soluongtontoida.ToString() + " cuon sach", "Thong bao")
            Return
        End If
        
        Dim i As Integer
        For i = 0 To ListDanhSach.Count - 1
            If (ListDanhSach(i).TenSach = PhieuSachDTO.TenSach And ListDanhSach(i).TheLoai = PhieuSachDTO.TheLoai And ListDanhSach(i).TacGia = PhieuSachDTO.TacGia) Then
                flay = False
            End If
        Next
        If flay = True Then
            Dim stt As Integer = dgvwDanhSachSach.RowCount
            Dim row As String() = {(stt + 1).ToString(), PhieuSachDTO.TenSach, PhieuSachDTO.TacGia, PhieuSachDTO.TheLoai, PhieuSachDTO.SoLuong, PhieuSachDTO.DonGia}
            dgvwDanhSachSach.Rows.Add(row)
            ListDanhSach.Add(PhieuSachDTO)
        Else
            MessageBox.Show("Sach nay da ton tai , ban co the sua thong tin", "Thong Bao")

        End If

    End Sub

    Private Sub dgvwDanhSachSach_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvwDanhSachSach.SelectionChanged
        If dgvwDanhSachSach.SelectedRows.Count() <= 0 Then
            Return
        End If
        txtSach.Text = dgvwDanhSachSach.SelectedRows(0).Cells(1).Value
        txtTacGia.Text = dgvwDanhSachSach.SelectedRows(0).Cells(2).Value
        ComboBox_TheLoai.Text = dgvwDanhSachSach.SelectedRows(0).Cells(3).Value
        txtSoLuong.Text = dgvwDanhSachSach.SelectedRows(0).Cells(4).Value
        txt_DonGia.Text = dgvwDanhSachSach.SelectedRows(0).Cells(5).Value

    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        If dgvwDanhSachSach.SelectedRows.Count() <= 0 Then
            Return
        End If
        ' xoa 1 dong trong ListDanhSach neu co phan tu do
        Dim i As Integer = 0
        For i = ListDanhSach.Count - 1 To 0 Step (-1)
            If ListDanhSach(i).TenSach = txtSach.Text.ToString() And ListDanhSach(i).TheLoai = ComboBox_TheLoai.Text And ListDanhSach(i).TacGia = txtTacGia.Text Then
                ListDanhSach.RemoveAt(i)
            End If


        Next
        ' xoa toan bo DataGListView
        dgvwDanhSachSach.Rows.Clear()
        ' Do toan bo du lieu trong ListDanhSach vao dataGrilview
        For i = 0 To ListDanhSach.Count - 1
            Dim row As String() = {(i + 1).ToString(), ListDanhSach(i).TenSach, ListDanhSach(i).TacGia, ListDanhSach(i).TheLoai, ListDanhSach(i).SoLuong, ListDanhSach(i).DonGia}
            dgvwDanhSachSach.Rows.Add(row)
        Next

    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If dgvwDanhSachSach.SelectedRows.Count() <= 0 Then
            Return
        End If
        ' Tao ra CPhieuNhapSach  Lay du lieu cua dong can sua    End Sub
        Dim CPHieuNhapSach As PHieuNhapSach = New PHieuNhapSach()
        CPHieuNhapSach.TenSach = dgvwDanhSachSach.SelectedRows(0).Cells(1).Value
        CPHieuNhapSach.TacGia = dgvwDanhSachSach.SelectedRows(0).Cells(2).Value
        CPHieuNhapSach.TheLoai = dgvwDanhSachSach.SelectedRows(0).Cells(3).Value
        CPHieuNhapSach.SoLuong = dgvwDanhSachSach.SelectedRows(0).Cells(4).Value
        CPHieuNhapSach.DonGia = dgvwDanhSachSach.SelectedRows(0).Cells(5).Value
        ' Lay dong da chon 
        Dim dongduocchon As Integer
        Dim i As Integer
        For i = 0 To ListDanhSach.Count - 1
            If (ListDanhSach(i).TenSach = CPHieuNhapSach.TenSach And ListDanhSach(i).TheLoai = CPHieuNhapSach.TheLoai And ListDanhSach(i).TacGia = CPHieuNhapSach.TacGia) Then
                dongduocchon = i
            End If
        Next
        '-------------------------Kiem tra du lieu sua-----------------------------------
        Dim kq As Integer = Kiemtrarong(txtSach.Text, txtTacGia.Text, ComboBox_TheLoai.Text, txtSoLuong.Text, txt_DonGia.Text)
        If (kq = 1) Then

            MessageBox.Show("Ten Sach Con trong kia")
            txtSach.Focus()
            Return
        End If
        If kq = 2 Then

            MessageBox.Show("Chua Nhap ten tac gia")
            txtSach.Focus()
            Return
        End If
        If (kq = 3) Then

            MessageBox.Show("Ban chua chon the loai")
            ComboBox_TheLoai.Focus()
            Return
        End If
        If kq = 4 Then

            MessageBox.Show(" So Luong con trong kia ban")
            txtSoLuong.Focus()
            Return
        End If
        ' Tao ra mot phieu sach de lay du lieu da thay doi

        Dim CPHieuSuaSach As PHieuNhapSach = New PHieuNhapSach()
        CPHieuSuaSach.TenSach = txtSach.Text
        CPHieuSuaSach.TacGia = txtTacGia.Text
        CPHieuSuaSach.TheLoai = ComboBox_TheLoai.Text
        CPHieuSuaSach.SoLuong = txtSoLuong.Text
        CPHieuSuaSach.DonGia = txt_DonGia.Text

        '-------------------Kiem tra so luong nhap phai lon hon so luong NHAP TOI THIEU---------------
        Dim soluong As Double = Double.Parse(txtSoLuong.Text)
        Dim ThamSoBUS = New ThamSoBUS()
        Dim SoLuongNhapToiThieu = ThamSoBUS.LaySoLuongNhapToiThieu()
        If (soluong < SoLuongNhapToiThieu) Then
            MessageBox.Show("So Luong Nhap Thap nhat phai la:" + SoLuongNhapToiThieu.ToString())
            txtSoLuong.Focus()
            Return

        End If
       

        ' --------- Lay so luong Ton tối đa------------------------------
        Dim soluongtontoida As Double
        soluongtontoida = ThamSoBUS.LaySoLuongTonToiDa()
        Dim soluongsachdanhap As Double = SachBUS.SoLuongTon(CPHieuSuaSach)
        ' Kiem tra
        If (soluongsachdanhap > soluongtontoida) Then
            MessageBox.Show(" Trong Tu Sach Da co :       " + soluongsachdanhap.ToString() + " cuon sach " + txtSach.Text + "   so voi so Luong ton toi da la :       " + soluongtontoida.ToString() + " cuon sach", "Thong bao")
            Return
        End If
        '-----------------------------
        ' cap nhat lai du lieu da thay doi
        For i = 0 To ListDanhSach.Count - 1
            If i <> dongduocchon Then
                If (ListDanhSach(i).TenSach = CPHieuSuaSach.TenSach And ListDanhSach(i).TheLoai = CPHieuSuaSach.TheLoai And ListDanhSach(i).TacGia = CPHieuSuaSach.TacGia) Then

                    MessageBox.Show("Sách Nay da Ton Tai Trong Phieu Nhập ", "Thong bao")
                    Return


                End If

            End If
            

        Next
        ListDanhSach(dongduocchon) = CPHieuSuaSach
        ' Xoa tat du lieu cua dataGridtView
        dgvwDanhSachSach.Rows.Clear()
        ' do du lieu tu ListDanhSach vao dataGridView
        For i = 0 To ListDanhSach.Count - 1
            Dim row As String() = {(i + 1).ToString(), ListDanhSach(i).TenSach, ListDanhSach(i).TacGia, ListDanhSach(i).TheLoai, ListDanhSach(i).SoLuong, ListDanhSach(i).DonGia}
            dgvwDanhSachSach.Rows.Add(row)
        Next
    End Sub

    Private Sub btn_LuuDuLieu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_LuuDuLieu.Click
        Dim NgayThang As DateTime = dt_NgayNhap.Text
        Dim Bus As PhieuNhapSachBUS = New PhieuNhapSachBUS()
        Dim maphieunhap As Integer = Bus.LuuPhieuNhapSach(NgayThang)
        ' Luu bang sach 
        Dim i As Integer
        For i = 0 To ListDanhSach.Count - 1

            Dim masach As Integer = SachBUS.LuuBangSach(ListDanhSach(i).TenSach, ListDanhSach(i).TheLoai, ListDanhSach(i).TacGia, ListDanhSach(i).DonGia, ListDanhSach(i).SoLuong, NgayThang)
            Dim ctpn As BangChiTietPhieuNhapSachDTO = New BangChiTietPhieuNhapSachDTO()
            
            ctpn.MaSach = masach
            ctpn.MaPhieuNhap = maphieunhap
            ctpn.SoLuongNhap = ListDanhSach(i).SoLuong
            ' Lu Vao Ban Chi Tiet Nhap Sach
            ChiTietPhieuNhapSachBUS.Them(ctpn)

        Next
        MessageBox.Show("Da Them Thanh Cong")
        dgvwDanhSachSach.Rows.Clear()
        TaoMoi()

    End Sub
End Class